import {Component} from '@angular/core';
import {NavController, ToastController} from 'ionic-angular';
import {WeatherHttp} from "./weatherhttp.service";
import {FormBuilder, FormGroup} from "@angular/forms";

import {ToastService} from "../../app/common/toast.service";

@Component({
  selector: 'page-weather',
  templateUrl: 'weather.html',
  providers: [WeatherHttp]
})
export class WeatherPage {
  public weatherFormGroup: FormGroup;
  public result: { data: { yesterday, city, aqi, forecast, ganmao, wendu }, status, desc };
  public forecastArray: Array<{ date, high, fengli, low, fengxiang, type }>;
  public cityName;
  public loadingFlag: boolean = false;

  constructor(public navCtrl: NavController, private weatherHttp: WeatherHttp, fb: FormBuilder, private toast: ToastService) {
    this.weatherFormGroup = fb.group({cityInput: '北京'});
  }

  onSubmitCity(form: any) {
    // console.log(">>>",form);
    this.cityName = form['cityInput'];
    this.loadingFlag = true;
    this.forecastArray = null;
    this.weatherHttp.searchWeather(this.cityName).subscribe(
      (response) => {
        console.log(response.json())
        this.result = response.json();
        this.forecastArray = this.result.data.forecast;
      },
      error => {
        this.toast.makeToast("查询失败", ToastService.MIDDLE_DURATION);
        console.error(error.status + "--" + error.statusText);
      },
      () => {
        console.log("请求完成")
        this.loadingFlag = false
      }
    );

  }

}
